#include "homepage.h"
#include "ui_homepage.h"
#include "globalval.h"

HomePage::HomePage(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::HomePage)
{
    ui->setupUi(this);

    //实例化四个界面
    m0Wig = new M0Wig;
    modbusWig = new ModbusWig;
    mbappWig = new MbappWig;
    setWig = new SetWig;

    //将四个界面依次添加到堆栈窗体
    //ui->stackedWidget->addWidget(m0Wig);
    //ui->stackedWidget->addWidget(modbusWig);
    //ui->stackedWidget->addWidget(mbappWig);
    ui->stackedWidget->addWidget(setWig);


    //连接列表界面和堆栈窗体的信号
    QObject::connect(ui->listWidget, SIGNAL(currentRowChanged(int)),
                     ui->stackedWidget, SLOT(setCurrentIndex(int)));

    //mqtt相关
    MqttHandler *mqtt = new MqttHandler;
    GlobalVal::mqtt = mqtt;


    //哪个界面需要接收Mqtt的消息，就订阅这个信号，然后在自己的界面文件中处理消息
    //    QObject::connect(mqtt, SIGNAL(dataUpSignal(QByteArray)),
    //                     modbusWig, SLOT(dataUpSlot(QByteArray)));
//    connect(mqtt, &MqttHandler::dataUpSignal,m0Wig,&M0Wig::mqttSlot);
    connect(mqtt, &MqttHandler::dataUpSignal,setWig,&SetWig::wendu);
}

HomePage::~HomePage()
{
    delete ui;
}

void HomePage::on_ipbutton_clicked()
{
    QString ip = ui->ipEdit->text();

    GlobalVal::mqtt->setHost(QHostAddress(ip));
    GlobalVal::mqtt->setPort(1883);
    GlobalVal::mqtt->connectToHost();
}

void HomePage::paintEvent(QPaintEvent *event)
{
    QPainter painter(this);
    int x = (width()-width()*0.5)/2;
    int y = (height()-width()*0.5)/2;
    int a = width()*0.5;
    QRadialGradient radia(rect().center(),width());
    radia.setColorAt(0,Qt::white);
    radia.setColorAt(1,QColor(255,204,255));
    painter.setBrush(radia);
    painter.drawRect(this->rect());
    painter.drawPixmap(QRect(x,y,a,a),QPixmap(":/pictures/真我.png"));
}

